package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("cargo/extCproduct")
public class ExtCproductController extends BaseController {
    @Reference
    private FactoryService factoryService;
    @Reference
    private ExtCproductService extCproductService;

    @RequestMapping("list")
    public String list(String contractId,
                       String contractProductId,
                       @RequestParam(defaultValue = "1") int pageNum,
                       @RequestParam(defaultValue = "5") int pageSize) {
        FactoryExample example = new FactoryExample();
        example.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(example);

        request.setAttribute("factoryList",factoryList);
        ExtCproductExample extCproductExample = new ExtCproductExample();
        ExtCproductExample.Criteria criteria = extCproductExample.createCriteria();
        criteria.andContractProductIdEqualTo(contractProductId);
        PageInfo<ExtCproduct> pageInfo = extCproductService.findByPage(extCproductExample, pageNum, pageSize);
        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);
        return "/cargo/extc/extc-list";
    }

    @RequestMapping("edit")
    public String edit(ExtCproduct extCproduct){
        String id = extCproduct.getId();
        extCproduct.setCompanyId(getLoginCompanyId());
        extCproduct.setCompanyName(getLoginCompanyName());

        if (StringUtils.isEmpty(id)){
            extCproductService.save(extCproduct);
        }else {
            extCproductService.update(extCproduct);
        }

        return "redirect:/cargo/extCproduct/list.do?contractId="
                +extCproduct.getContractId()+"&contractProductId="+extCproduct.getContractProductId();
    }

    /**
     * 3. 进入修改页面
     * 请求地址：http://localhost:8080/cargo/extCproduct/toUpdate.do
     * 请求参数：
     *      id                  附件id
     *      contractId          购销合同
     *      contractProductId   货物
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id,String contractId,String contractProductId){
        // 查询附件厂家
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);

        // 根据附件id查询
        ExtCproduct extCproduct = extCproductService.findById(id);

        // 保存
        request.setAttribute("factoryList",factoryList);
        request.setAttribute("extCproduct",extCproduct);
        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);

        return "cargo/extc/extc-update";
    }

    /**
     * 4. 删除
     */
    @RequestMapping("/delete")
    public String delete(String id,String contractId,String contractProductId) {
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId="
                +contractId+"&contractProductId="+contractProductId;
    }


}
